package com.demo.cms.webapp.template.directive;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.demo.cms.model.ArticleComment;
import com.demo.cms.service.ArticleCommentService;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Site;
import com.demo.webapp.context.MultisiteContextHolder;
import com.demo.webapp.views.freemarker.directive.core.PageContextDirectiveSupport;

import freemarker.core.Environment;
import freemarker.core.Environment.TemplateDirectiveBodyEx;
import freemarker.template.SimpleHash;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

/**
 * 评论列表指令
 * 通过文章ID查询指定评论
 * @author ryuu.kk
 *
 */
public class CommentListDirective extends PageContextDirectiveSupport {
	
	/**
	 * 页面模板名
	 */
	private static final String COMMENT_LIST_TEMPLATE_NAME = "classpath:/com/demo/cms/webapp/template/commentList.ftl";
	
	/**
	 * 文章评论服务
	 */
	@Autowired
	private ArticleCommentService articleCommentService;

	@SuppressWarnings("rawtypes")
	@Override
	public void preWriter(Environment env, TemplateDirectiveBodyEx bodyEx,
			TemplateModel[] loopVars, SimpleHash hash, Map params)
			throws IOException, TemplateException {
		
		// 评论模型
		List<ArticleComment> commentList = new ArrayList<ArticleComment>(0);
		// 文章ID
		Long articleId = null;
		Integer limit;
		Integer sort;
		articleId = getLong("articleId", params);
		
		Site site = MultisiteContextHolder.getSite();
		Long siteId = site == null ? null : site.getId();
		
		// 取得检索大小
		limit = getInteger("limit", params);
		if (limit == null) {
			// 默认10条
			limit = new Integer(10);
		}
		
		// 取得排序类型
		sort = getInteger("sort", params);
		if (sort == null) {
			sort = 1;
		}
		SearchArgumentSupport args = new SearchArgumentSupport(0, limit);
		
		switch (sort) {
		// 排序方式  1:ID升序(默认); 2:ID降序; 
		case 2:
			// 栏目ID降序
			args.setOrders(new String[]{" ID DESC "});
			break;
		default:
			// 栏目ID顺序
			args.setOrders(new String[]{" ID ASC "});
		}
		
		// 栏目列表查询（审核通过）
		ResultSearchResult<ArticleComment> results = articleCommentService.search(siteId, articleId, ArticleComment.TYPE_COMMENT, args);
		if (!results.isEmpty()) {
			commentList = new ArrayList<ArticleComment>(results.getResults());
		}
		hash.put("articleId", articleId);
		// 评论列表
		hash.put("commentList", commentList);
	}

	@Override
	public void afterInvoke(Writer writer) {
		
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String getTemplatePath(Map params) {
		String path = "";
		try {
			path = getString("path", params, COMMENT_LIST_TEMPLATE_NAME);
		} catch (TemplateModelException e) {}
		return path;
	}
}
